/**
 * Created by 齐力锋 on 2017/5/21.
 */
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.*;
import java.util.Random;


public class CreateQuestion {
    private final char ADD = '+';

    private final char SUBTRACT = '-';

    private final char MULTIPLY = '*';

    private final char DIVIDE = '/';

    private Stack<Integer> stack;
    Random ran = new Random();
    private String sym, Ti = "";
    private int num, trues = 0;
    private List<String> list, list1, list2;

    public CreateQuestion() {
        list = new ArrayList<String>();
        list1 = new ArrayList<String>();
        list2 = new ArrayList<String>();
    }

    public void Ti(int number, int many) {
        many = Ticlass(many);
        for (int j = 0; j < number; j++) {
            String ti = "";
            for (int i = 0; i < many; i++) {
                int A = ran.nextInt(20) + 1;
                int B = ran.nextInt(5);
                if (parity(i)) {
                    if (list1.indexOf("( ") == -1)
                        list1.add(getSym() + " ");
                    else if (list1.size() - list1.lastIndexOf("( ") > 4) {
                        if (list1.lastIndexOf(") ") - list1.lastIndexOf("( ") < 0 && B == 0) {
                            list1.add(") ");
                            list1.add(getSym() + " ");
                        } else list1.add(getSym() + " ");
                    } else list1.add(getSym() + " ");
                } else if (i == many - 1) {
                    if (list1.lastIndexOf("( ") - list1.lastIndexOf(") ") > 0) {
                        list1.add(A + " ");
                        list1.add(") ");
                    } else list1.add(A + " ");
                } else if (list1.lastIndexOf(") ") != -1)
                    if (list1.lastIndexOf(") ") - list1.lastIndexOf("( ") > 0 && B == 0) {
                        list1.add("( ");
                        list1.add(A + " ");
                    } else list1.add(A + " ");
                else if (list1.indexOf("( ") == -1 && B == 0) {
                    list1.add("( ");
                    list1.add(A + " ");
                } else list1.add(A + " ");
            }
            for (String i : list1)
                ti += i;
            list1.clear();
            list.add(ti);
        }
    }

    public void show() {
        transform trans = new transform();

        Scanner scan = new Scanner(System.in);
        ListIterator<String> li = list.listIterator();
        while (li.hasNext()) {
            String A = li.next();
            li.remove();

            trans.transform();


            System.out.print(A + " =");
            int B = scan.nextInt();

            if ( evalSingleOp('+',3,3)== B) {
                System.out.println("恭喜你正确！");
                trues++;
            } else {
                System.out.printf("错误！");
                System.out.println("正确答案为：" + evalSingleOp('+',3,3));
            }
        }
    }



    public int getTrues() {
        return trues;
    }

    public String getSym() {
        int A = ran.nextInt(3);
        switch (A) {
            case 0:
                sym = "+";
                break;
            case 1:
                sym = "-";
                break;
            case 2:
                sym = "*";
                break;
        }
        return sym;
    }

    public boolean parity(int num) {
        if (num % 2 == 1)
            return true;
        else
            return false;
    }

    public int Ticlass(int many) {
        int A = 1;
        for (int i = 0; i < many; i++) {
            A += 2;
        }
        return A;
    }

    private boolean isOperator(String token) {
        return (token.equals("*") || token.equals("/") ||
                token.equals("+") || token.equals("-"));
    }
    public int evalSingleOp (char operation, int op1, int op2)
    {
        int result = 0;

        switch (operation)
        {
            case ADD:
                result = op1 + op2;
                break;
            case SUBTRACT:
                result = op1 - op2;
                break;
            case MULTIPLY:
                result = op1 * op2;
                break;
            case DIVIDE:
                result = op1 / op2;
        }

        return result;
    }
}

